
//
//  HMPictureView.swift
//  Weibo
//
//  Created by 李学林 on 15/12/11.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit
import SnapKit

// 每一个条目之前的间距
private let itemMargin: CGFloat = 5
// 每一个条目的宽高
private let itemWH = (SCREENW - itemMargin * 2 - HMStatusCellMargin * 2) / 3


class HMPictureView: UICollectionView {
// item的重用id
    let HMPictureViewItemID = "HMPictureViewItemID"
    
    var sizeConstraint: Constraint?
    
    var pic_urls: [HMStatusPictureInfo]?{
        didSet{
            countLabel.text = "\(pic_urls?.count ?? 0)"
            let size = picturesSize(pic_urls?.count ?? 0)
            self.snp_updateConstraints { (make) -> Void in
                self.sizeConstraint = make.size.equalTo(size).constraint
        
            }
            reloadData()
        }
        
    
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
        
        
        // 设置 collectionView 相关属性
        //注册
        registerClass(HMStatusPictureViewCell.self, forCellWithReuseIdentifier: HMPictureViewItemID)
        // 设置数据源代理
        dataSource = self
        //
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(countLabel)
        
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    
    
    
    // 计算pictureView的SIZE
    private func picturesSize(count: Int) ->CGSize{
    
               // 列
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        // 行
        let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
    
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: width, height: height)
    }
    
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 40)
    
}


extension HMPictureView : UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pic_urls?.count ?? 0
        
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = dequeueReusableCellWithReuseIdentifier(HMPictureViewItemID, forIndexPath: indexPath) as! HMStatusPictureViewCell
        
        cell.backgroundColor = RandomColor()
        cell.imageViewInfo = pic_urls![indexPath.row]
        return cell
        
    }
    
}

class HMStatusPictureViewCell: UICollectionViewCell {
    //加载数据-------弱点
    var imageViewInfo : HMStatusPictureInfo?{
        
        didSet{
            if let urlString = imageViewInfo?.thumbnail_pic {
                
                if urlString .hasSuffix(".gif"){
                
                    gifImageView.hidden = false
                }else{
                    gifImageView.hidden = true
                }
                
                
                imageView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "timeline_retweet_background"))
            }
            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI(){
        
    
        // 添加子控件上
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        // 添加约束
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsMake(0, 0, 0, 0))
            
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
        
    }
    
 /// 懒加载CELL上的imageview
    private lazy var imageView : UIImageView = {
        
        let imageView =  UIImageView()
        
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        
        imageView.clipsToBounds = true
        
        return imageView
    }()
    
    // 懒加载gif图标
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    
}

